package com.flower.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flower.annotation.Log;
import com.flower.domain.vo.farmer.FlowerEncyclopediaDetailVO;
import com.flower.domain.vo.farmer.FlowerEncyclopediaVO;
import com.flower.service.FlowerEncyclopediaService;
import com.flower.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理端花卉审核
 */
@RestController
@RequestMapping("/admin/flower/audit")
public class FlowerAuditController {
    
    @Autowired
    private FlowerEncyclopediaService encyclopediaService;
    
    /**
     * 获取待审核的花卉百科列表
     */
    @GetMapping("/pending")
    public AjaxResult getPendingAuditList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        
        Page<FlowerEncyclopediaVO> pageResult = encyclopediaService.getEncyclopediaAuditsPage(page, pageSize, keyword);
        
        // 检查每个花卉是否有待审核的更新
        pageResult.getRecords().forEach(item -> {
            boolean hasUpdate = encyclopediaService.hasPendingUpdate(item.getId());
            item.setHasPendingUpdate(hasUpdate);
        });
        
        return AjaxResult.success(pageResult);
    }
    
    /**
     * 审核通过花卉百科
     */
    @Log(name = "审核通过花卉百科")
    @PutMapping("/approve/{id}")
    public AjaxResult approveEncyclopedia(@PathVariable Long id) {
        boolean result = encyclopediaService.updateEncyclopediaStatus(id, 1); // 1: 已发布
        
        if (result) {
            return AjaxResult.success("审核通过成功");
        } else {
            return AjaxResult.error("审核失败，可能是花卉不存在或没有待审核的更新");
        }
    }
    
    /**
     * 拒绝花卉百科
     */
    @Log(name = "拒绝花卉百科")
    @PutMapping("/reject/{id}")
    public AjaxResult rejectEncyclopedia(@PathVariable Long id, @RequestParam(required = false) String reason) {
        boolean result = encyclopediaService.rejectEncyclopedia(id, reason);
        
        if (result) {
            return AjaxResult.success("拒绝审核成功");
        } else {
            return AjaxResult.error("操作失败，可能是花卉不存在或没有待审核的更新");
        }
    }
    
    /**
     * 获取花卉百科详情（审核用）
     */
    /*@GetMapping("/detail/{id}")
    public AjaxResult getAuditDetail(@PathVariable Long id) {
        FlowerEncyclopediaDetailVO detailVO = encyclopediaService.getPendingEncyclopediaFromRedis(id);
        if (detailVO == null) {
            return AjaxResult.error("未找到指定花卉信息");
        }
        detailVO.setId(id);
        return AjaxResult.success(detailVO);
    }*/
    
    /**
     * 获取花卉百科待审核的更新详情
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getPendingUpdateDetail(@PathVariable Long id) {
        // 先检查是否有待审核的更新
        if (!encyclopediaService.hasPendingUpdate(id)) {
            return AjaxResult.error("该花卉没有待审核的更新");
        }
        
        // 获取待审核的更新数据
        FlowerEncyclopediaDetailVO pendingDetail = encyclopediaService.getPendingEncyclopediaFromRedis(id);
        if (pendingDetail == null) {
            return AjaxResult.error("获取待审核的更新数据失败");
        }
        
        // 获取当前的花卉数据
        FlowerEncyclopediaDetailVO currentDetail = encyclopediaService.getEncyclopediaDetail(id);
        
        // 组装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("current", currentDetail);
        result.put("pending", pendingDetail);
        
        return AjaxResult.success(result);
    }
} 